/**
 * 保存投放图片广告 
 */
$(function(){
	//查出名称id图片路径
	$("#getQueryCrashLog").on("click",function(){
		var optionsDev=null;
		var adId = $("#adId").val();
		if(adId!=undefined){
			 alert("您已经添加了一条将要发布的广告,请删除之后再次添加");
			 return false;
		 }
		var pictureSaveUrl = "http://114.116.41.171:8089/pictures/";//服务器保存图片的路径
		//var pictureSaveUrl = "http://192.168.3.67:8089/pictures/";//服务器保存图片的路径
		 var type = null;
	       $.ajax({  
	            async : false,  
	            cache:false,  
	            type: 'POST',  
	            dataType : "json",  
	            url: '/Lvscore_Advertiser_Maven/finishPlacementAdByAdvId',  //请求的路径  
	            success:function(data){//controller层传过来的map 
	            	var beans = data.beans;
	            	var companyName = data.companyName;
	            	var companyLogo = data.companyLogo;
	            	$("#cpLogUrl").attr("src",pictureSaveUrl+companyLogo);
	            	$("#companyName").text(companyName);
	            	for(var j = 0; j < beans.length;j++){
	            		optionsDev+="<option value=\'"+j+"' data-adName=\'"+beans[j].name+"' data-img=\'"+beans[j].adHomepage+"' data-id=\'"+beans[j].id+"' data-type=\'"+beans[j].terminalType+"'\>"+beans[j].name+"</option>";
	            		type = beans[j].terminalType;
	            	}
	             }   
	        });   
	       $("#adv").html(optionsDev);
		})
	//验证
	function check(){
		//总金额
		var releaseAmount = $("#adTotlePrice").val();
		var deliveryPrice = $("#adPrice").val();
		var channelAmount = 0.0;
		 var beans = $("input[name='plTotlePrice']");
			 for(var j = 0; j < beans.length;j++){
				 channelAmount += beans[j].value*1;
				 var amount = channelAmount-beans[j].value*1;
				 var money = releaseAmount*1-amount;
				 if(parseInt(channelAmount)>parseInt(releaseAmount)){
					 alert("您还有"+money+"元可用");
					 beans[j].value="";
					 return false;
				 }
				 if(channelAmount==releaseAmount){
					 alert("您还有0元可用");
					 return false;
				 }
			 }
		if(releaseAmount==""||releaseAmount==null){
			alert("请先添加广告总金额");
			return false;
		}
		if(deliveryPrice==""||deliveryPrice==null){
			alert("请先添加广告单价");
			return false;
		}
			}
	//查出所有渠道
	$("#getApprovider").on("click",function(){
		var optionsDev="null";
		//总金额
		var releaseAmount = $("#adTotlePrice").val();
		//单价
		var deliveryPrice = $("#adPrice").val();
		var channelAmount = 0.0;
		//账户余额
		var adMoney = $("#actualBalance").html();
		var UsMoney = adMoney-releaseAmount;
		if(UsMoney<0){
			alert("您的账户余额不足");
			$("#adTotlePrice").prop("value","");
			return false;
		}
		var placement = $("input[name='id']");
		 var beans = $("input[name='plTotlePrice']");
			 for(var j = 0; j < beans.length;j++){
				 channelAmount += beans[j].value*1;
				 var amount = channelAmount-beans[j].value*1;
				 var money = releaseAmount*1-amount;
				 if(parseInt(channelAmount)>parseInt(releaseAmount)){
					 alert("您还有"+money+"元可用");
					 beans[j].value="";
					 return false;
				 }
				 if(channelAmount==releaseAmount){
					 alert("您还有0元可用");
					 return false;
				 }
			 }
		if(releaseAmount==""||releaseAmount==null){
			alert("请先添加广告总金额");
			return false;
		}
		if(deliveryPrice==""||deliveryPrice==null){
			alert("请先添加广告单价");
			return false;
		}
		var ajax = $.ajax({  
            async : false,  
            cache:false,  
            type: 'POST',  
            dataType : "json",  
            url: '/Lvscore_Advertiser_Maven/finishApprovider',  //请求的路径  
            success:function(data){//controller层传过来的map 
//            	var placement = $("input[name='id']");
            	var beans = data.beans;
            	//选过之后的不能再选(渠道)
            	for(var i = 0; i < placement.length;i++){
	       			for(var j = 0; j < beans.length;j++){
	       				if(placement[i].value==beans[j].id){
	       					beans.splice([j],1);
	       				}
	       			}
       		 	}
            	for(var j = 0; j < beans.length;j++){
            		optionsDev+="<option value=\'"+j+"' data-adName=\'"+beans[j].name+"' data-img=\'"+beans[j].logoUrl+"' data-id=\'"+beans[j].id+"'\>"+beans[j].name+"</option>";
            	}
             }   
        });
       $("#advApp").html(optionsDev);
	});
	/**
	 * 添加渠道 
	 */
	$("#throwButt").click(function (){
		var planStart = $("#startTime").val();
		var planEnd = $("#endTime").val();
		//广告
		var adId = $("#adId").val();
		var releaseAmount = $("#adTotlePrice").val();
		var deliveryPrice = $("#adPrice").val();
		//成本价
		var putPrice = $("#adPut").val();
		//判断渠道单价
		  var dPrice = $("input[name='plPrice']");
		  for(var j = 0; j < dPrice.length;j++){
				 if(dPrice[j].value==""||dPrice[j].value==null){
					 dPrice[j].value = deliveryPrice;
				 }
			 }
		//渠道名称
		var channelName="";
		$("input[name='plName']").each(
				 function(){
					 if($(this).val()!=""||$(this).val()!=null){
						 channelName += $(this).val()+','
					}
				 } 
		 )
		 //渠道总金额
		 var channelAmount="";
		 $("input[name='plTotlePrice']").each(  
				 function(){  
					 channelAmount += $(this).val()+',';
				 } 
		 )
		 //渠道单价
		 var channelPrice="";
		 $("input[name='plPrice']").each(  
				 function(){  
					 channelPrice += $(this).val()+',';
				 } 
		 )
		 //渠道图路径
		 var logoUrl="";
		 $("input[name='img']").each(  
				 function(){  
					 logoUrl += $(this).val()+',';
				 } 
		 )
		 //渠道名对应外键id
		 var appId = "";
		 $("input[name='id']").each(  
				 function(){  
					 appId += $(this).val()+',';
				 } 
		 )
		 //渠道载体类型
		 var deliveryCarrier = $("#deliveryCarrier").val();
		 var param = {
					"planStart" : planStart,
					"planEnd" : planEnd,
					"adId" : adId,
					"releaseAmount" : releaseAmount,
					"deliveryPrice" : deliveryPrice,
					"putPrice" : putPrice,
					"channelName" : channelName,
					"channelAmount" : channelAmount,
					"channelPrice" : channelPrice,
					"deliveryCarrier" : deliveryCarrier,
					"logoUrl" : logoUrl,
					"appId" : appId
				}
		 if(planStart>planEnd){
			 alert("日期输入有误，请让起始时间小于截止时间");
			 $("#startTime").prop("value","");
			 $("#endTime").prop("value","");
			 return false;
		 }
		 if(planStart==""||planStart==null||planEnd==""||planEnd==null){
			 alert("日期输入有误不能为空");
			 return false;
		 }
		//账户余额
			var adMoney = $("#actualBalance").html();
			var UsMoney = adMoney-releaseAmount;
			if(UsMoney<0){
				alert("您的账户余额不足");
				$("#adTotlePrice").prop("value","");
				return false;
			}
		 if(parseInt(releaseAmount)<parseInt(deliveryPrice)){
			 alert("广告总额不能小于单价");
			 $("#adPrice").prop("value","");
			 return false;
		 }
		 
		//总金额
			var releaseAmount = $("#adTotlePrice").val();
			var deliveryPrice = $("#adPrice").val();
			var channelAmount = 0.0;
			 var beans = $("input[name='plTotlePrice']");
				 for(var j = 0; j < beans.length;j++){
					 channelAmount += beans[j].value*1;
					 var amount = channelAmount-beans[j].value*1;
					 var money = releaseAmount*1-amount;
					 if(parseInt(channelAmount)>parseInt(releaseAmount)){
						 alert("您还有"+money+"元可用");
						 beans[j].value="";
						 return false;
					 }
					 if(beans[j].value==""||beans[j].value==null){
						 alert("渠道总金额不能为空");
						 return false;
					 }
				 }
			if(releaseAmount==""||releaseAmount==null){
				alert("广告总金额不能为空");
				return false;
			}
			if(putPrice==""||putPrice==null){
				alert("广告成本价不能为空");
				return false;
			}
			if(deliveryPrice==""||deliveryPrice==null){
				alert("广告单价不能为空");
				return false;
			}
			if(parseInt(deliveryPrice)<parseInt(putPrice)){
				alert("单价不能小于成本价");
				$("#adPrice").prop("value","");
				return false;
			}
			for(var j=0;j<beans.length;j++){
				var money = beans[j].value-dPrice[j].value;
				if(money<0){
					alert("渠道总金额不能小于单价");
					dPrice[j].value = "";
					return false;
				}
			}
			$("#throwButt").prop("disabled","disabled");
		$.ajax({
			url : '/Lvscore_Advertiser_Maven/savePicturePlacementAd',
	        data:param,
			type : 'post',
			success : function(data) {
				var check = data.check;
				alert(check);
			location.href ="/Lvscore_Advertiser_Maven/advertiserWeb/views/adManagement/pictureAd/index.html?selectedNav%3D2%26navPos%3D2";
			},
	        error: function(){}        
		});
	})
	
	
//	function finishPlacementAd(){
//		$.ajax({
//			url : '/Lvscore_Advertiser_Maven/finishPlacementAd',
//			type : 'post',
//			success : function(data) {
//				alert("okkkkkkkkk");
//			}    
//		});
//	};
	//实现删除
	/*$(".delete").on("click",function(){
		$(this).parent("p").remove();
		 
	});*/
	$("#channelSelect,#adSelect").on("click",".delete",function(){
		$(this).parent("p").remove();
		$("#typeImg").prop("src","");
  	   	$("#deliveryCarrier").val("");
  	   	$("#typeDelivey").text("");
	})
	
	$("#selectAd").on("click",function(){
		selectAd();
	});
	$("#selectAdDitch").on("click",function(){
		selectAdDitch();
	});
var pictureSaveUrl = "http://114.116.41.171:8089/pictures/";//服务器保存图片的路径
//var pictureSaveUrl = "http://192.168.3.67:8089/pictures/";//服务器保存图片的路径
     function selectAd(){
	     //var dom = $("#selectAd").parent("div.form-group").siblings("select.selectAdModalOption");
	     var dom=$("#adv");
		 if(dom.val()){	
             var valDom = dom.children("option").eq(dom.val());
             var name=valDom.attr("data-adName");
             var id=valDom.attr("data-id");
			 var price = valDom.attr("data-price");
			 var total = valDom.attr("data-total");
			 var img = valDom.attr("data-img");
			 var type = valDom.attr("data-type");
		       if(type=='pad'){
		    	  $("#typeImg").prop("src","/Lvscore_Advertiser_Maven/advertiserWeb/static/images/padIcon.png");
		    	  $("#deliveryCarrier").val("pad");
		    	  $("#typeDelivey").text("pad");
		       }
		       if(type=='pc'){
		    	   $("#typeImg").prop("src","/Lvscore_Advertiser_Maven/advertiserWeb/static/images/pcIcon.png");
		    	   $("#deliveryCarrier").val("pc");
		    	   $("#typeDelivey").text("pc");
		       }
		       if(type=='mobile'){
		    	   $("#typeImg").prop("src","/Lvscore_Advertiser_Maven/advertiserWeb/static/images/mobileIcon.png");
		    	   $("#deliveryCarrier").val("mobile");
		    	   $("#typeDelivey").text("mobile");
		       }
		       if(type=='tv'){
		    	   $("#typeImg").prop("src","/Lvscore_Advertiser_Maven/advertiserWeb/static/images/tvIcon.png");
		    	   $("#deliveryCarrier").val("tv");
		    	   $("#typeDelivey").text("tv");
		       }
			/* console.log(price);
			 console.log(total);
			 console.log(img);*/
			 var itemHtml = '<p><img src="'+pictureSaveUrl+img+'">'+ 
			 '<input id="adName" value="'+name+'" class="advertisement" readonly="readonly"><input id="adId" value="'+id+'" class="money" type="text" hidden> 投放总金额 <input id="adTotlePrice" value="" class="money" type="text">'+ 
			 '<b>(元)</b>成本价<input id="adPut" class="price" value="" type="text"><b>(次/元)</b>单价<input id="adPrice" class="price" value="" type="text"><b>(次/元)</b>'+ 
			 '<input value="x&nbsp;删除" class="delete" type="button"></p>';
			 //selectAdDom.siblings(".ADselect").eq(0).append(itemHtml);
			 $("#adSelect").append(itemHtml);
			 $("#selectAd").next().trigger("click");
		 }else{
		     alert("您尚未选择广告，请选取后再操作！")
		 }				 					 				     
	 };
	 
	 //222222222222
	 function selectAdDitch(){
	     //var dom = ele.parent("div.form-group").siblings("select.selectAdModalOption");
		 var dom=$("#advApp");
		 if(dom.val()){	
             var valDom = dom.children("option").eq(dom.val()),name=valDom.attr("data-adName"),
			 price = valDom.attr("data-price"),total = valDom.attr("data-total"),img = valDom.attr("data-img"),id = valDom.attr("data-id");						 
			 var itemText = '<p><img src="'+pictureSaveUrl+img+'">'+ 
			 '<input name="plName" value="'+name+'" class="advertisement" type="button"><input name="id" value="'+id+'" class="advertisement" type="button" hidden><input name="img" value="'+img+'" class="advertisement" type="button" hidden> 投放总金额 <input name="plTotlePrice" value="" class="money" type="text">'+ 
			 '<b>(元)</b>单价<input name="plPrice" class="price" value="" type="text"><b>(次/元)</b>'+ 
			 '<input value="x&nbsp;删除" class="delete" type="button"></p>';
			 $("#channelSelect").append(itemText);
			 $("#selectAdDitch").next().trigger("click");
		 }else{
		     alert("您尚未选择广告，请选取后再操作！")
		 }				 					 				     
	 };
     /* $("#startTime,#endTime").datetimepicker({
         minView: "month", 
         language:  'zh-CN',
         format: 'yyyy-mm-dd',
         todayBtn:  1,
         autoclose: 1,
	 }); */
	/*$(".add").click(function(){
		$(".adLayoutContCenter", parent.document).empty();
		var getHtml = $(".qudaoAdLayout").html();
		$(".adLayoutContTop", parent.document).find("p").text("选择需要投放的渠道");
		$("#sure2", parent.document).show();
		$("#sure1", parent.document).hide();
		$(".adLayoutContCenter", parent.document).append(getHtml);
		$(".adLayoutBox", parent.document).show();
		$("body", parent.document).css("overflow", "hidden");		 			 
	});*/
	
})
	/**
	 * 判断结束时间 
	 */
	function checkEndTime(){
	var planStart = $("#startTime").val();
	var planEnd = $("#endTime").val();
	if(planStart==""||planStart==null||planEnd==""||planEnd==null){
		 alert("日期输入有误不能为空");
		 return false;
	 }
	if(planStart>planEnd&&planEnd!=null&&planEnd!=""){
		 alert("日期输入有误，请让起始时间小于截止时间");
		 $("#endTime").prop("value","");
		 return false;
	 }
	}
	/**
	 * 现在时间格式转换
	 */
	function show(){
	   var mydate = new Date();
	   var mounth;
	   var day;
	   if(mydate.getMonth()+1<10){
		   mounth = "0"+(mydate.getMonth()+1);
	   }else{
		   mounth = mydate.getMonth()+1;
	   }
	   if(mydate.getDate()<10){
		   day = "0"+mydate.getDate();
	   }else{
		   day = mydate.getDate();
	   }
	   var str = "" + mydate.getFullYear() + "-";
	   str += mounth + "-";
	   str += day;
	   return str;
	  }
	/**
	 * 判断起始时间 
	 */
	function checkStartTime(){
		var planStart = $("#startTime").val();
		var planEnd = $("#endTime").val();
		var nowTime = show();
		if(planStart==""||planStart==null){
			 alert("日期输入有误不能为空");
			 return false;
		 }
		if(planStart<nowTime){
			alert("起始时间不能小于今天");
			$("#startTime").prop("value","");
		}
		if(planStart>planEnd&&planEnd!=null&&planEnd!=""){
			 alert("日期输入有误，请让起始时间小于截止时间");
			 $("#startTime").prop("value","");
			 return false;
		 }
	}